package com.wenlin.myproduction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.base.BaseActivity;
import com.wenlin.myproduction.base.BaseSwipeActivity;
import com.wenlin.myproduction.bean.NewPlanEn;
import com.wenlin.myproduction.bean.TeacherEn;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.ToastUtils;
import com.wenlin.myproduction.util.TypeData;
import com.wenlin.myproduction.util.TypeDataNames;
import com.wenlin.myproduction.util.ValCommon;
import com.wenlin.myproduction.widget.BookBannerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BannerViewActivity extends BaseSwipeActivity {

    @BindView(R.id.banner)
    BookBannerView bookBannerView;

    private int progress;
    private int latestUnlockIndex;
    private List<NewPlanEn> planList;

    @Override
    public int getContentView() {
        return R.layout.activity_banner_view;
    }

    public static void start(BaseActivity activity) {
        Intent intent = new Intent(activity, BannerViewActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(ValCommon.BANNER_VIEW);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        bookBannerView.setCallback(new BookBannerView.callback() {
            @Override
            public void onClickWithUnlock(NewPlanEn planEn) {
                BookActivity.start(BannerViewActivity.this, planEn);
            }

            @Override
            public void onClickWithLock(String title) {
                ToastUtils.getInstance().showInfo("请先完成《" + title + "》的学习");
                if (Checker.notNull(bookBannerView)) bookBannerView.setCurrentUnlockItem(latestUnlockIndex);
            }

            @Override
            public void onClickTodayCanNotRead() {
                ToastUtils.getInstance().showInfo("今日课程均已解锁，明天再来吧~");
                if (Checker.notNull(bookBannerView)) bookBannerView.setCurrentUnlockItem(latestUnlockIndex);
            }

            @Override
            public void onClickLoading(String msg) {
                ToastUtils.getInstance().showInfo(msg);
            }
        });
        reloadData();
    }

    private void reloadData() {
        //fake data 使用本地的假数据
        if (Checker.hasNoList(planList)) {
            planList = new ArrayList<>();
            NewPlanEn p1 = new NewPlanEn();
            p1.StudyPlanName = "Do Not Litter";
            p1.CoverPic = "http://resimg.iqeq.cn/myenglish/res/2020/7/14/taKH7ytM4kkzXajwBMbA2R8tcdQZ8wtj.jpg";
            p1.TeacherInfo = new TeacherEn("Anna", "");
            planList.add(p1);
            NewPlanEn p2 = new NewPlanEn();
            p2.StudyPlanName = "I Can Skate";
            p2.CoverPic = "http://resimg.iqeq.cn/myenglish/res/2020/7/14/b4NnAyJKf7nJdbiazEmiNydDsMERfQRp.jpg";
            p2.TeacherInfo = new TeacherEn("Anna", "");
            planList.add(p2);
            NewPlanEn p3 = new NewPlanEn();
            p3.StudyPlanName = "Trucks";
            p3.CoverPic = "http://resimg.iqeq.cn/myenglish/res/2020/7/14/C7Mx3hme6PKQEzwY6MBhMhAdZjn8QcP7.jpg";
            p3.TeacherInfo = new TeacherEn("Anna", "");
            planList.add(p3);
            NewPlanEn p4 = new NewPlanEn();
            p4.StudyPlanName = "Animals Habitats";
            p4.CoverPic = "http://resimg.iqeq.cn/myenglish/res/2020/7/14/aMmfH3pCHcETR2QHD5cZDdMYTRxfpdaf.jpg";
            p4.TeacherInfo = new TeacherEn("Anna", "");
            planList.add(p4);
        }
        List<BookBannerView.BannerBean> list = new ArrayList<>();
        for (int i = 0; i < planList.size(); i++) {
            NewPlanEn p = planList.get(i);
            list.add(new BookBannerView.BannerBean(p.StudyPlanName, p.TeacherInfo.Name, p.CoverPic, p.TeacherInfo.HeadUrl, i, i <= latestUnlockIndex, i < planList.size() - 1, p));
        }
        if (Checker.notNull(bookBannerView)) bookBannerView.resetData(list, latestUnlockIndex);
    }

    private void clearData() {
        if (Checker.notNull(bookBannerView)) bookBannerView.resetData(null);
        latestUnlockIndex = 0;
        progress = 0;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void threadHandle(TypeData typeData) {
        String name = typeData.getName();
        if (name.equals(TypeDataNames.finishBook)) {
            NewPlanEn newPlanEn = (NewPlanEn) typeData.getData();
            if (Checker.isNull(newPlanEn)) return;
            if (Checker.isNull(bookBannerView)) return;
            List<NewPlanEn> list = bookBannerView.getOriPlanData();
            if (Checker.hasNoList(list)) return;
            boolean unlock = false;
            for (int i = 0; i < list.size(); i++) {
                NewPlanEn p = list.get(i);
                if (unlock) {//这里的2暂时写死，理应通过后台设置每日阅读书本数量上限
                    if (i <= 2)                    latestUnlockIndex = i;
                    else ToastUtils.getInstance().showInfo("明天再学吧~");
                    break;
                } else if (p.CoverPic.equals(newPlanEn.CoverPic) || p.StudyPlanName.equals(newPlanEn.StudyPlanName)) {//书本名称及封面相同则视为同一个对象
                    unlock = true;
                }
            }
            reloadData();
        }
    }

    @OnClick({R.id.tv_load_data, R.id.tv_clear_data, R.id.tv_not_yet, R.id.tv_progress, R.id.tv_wait_to_share, R.id.tv_complete})
    void click(View view) {
        switch (view.getId()) {
            case R.id.tv_load_data:
                reloadData();
                break;
            case R.id.tv_clear_data:
                clearData();
                break;
            case R.id.tv_not_yet:
                progress = 0;
                if (Checker.notNull(bookBannerView)) bookBannerView.setPunchNotYet();
                break;
            case R.id.tv_progress:
                progress += 10;
                if (progress >= 100) progress -= 100;
                if (Checker.notNull(bookBannerView)) bookBannerView.setProgress(progress);
                break;
            case R.id.tv_wait_to_share:
                progress = 90;
                if (Checker.notNull(bookBannerView)) bookBannerView.setPunchWaitToShare();
                break;
            case R.id.tv_complete:
                progress = 100;
                if (Checker.notNull(bookBannerView)) bookBannerView.setPunchCompleted();
                break;
            default:
        }
    }
}
